package com.xinnet.core.validate;

import java.util.Properties;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.xinnet.core.validate.attribute.AlphaNum;


/**
 * 半角英数字(alphanum2)用的Validator类。
 * 
 * @author wenjianye 请输入半角英数字。
 */
public class AlphaNumValidator  extends Validator  implements ConstraintValidator<AlphaNum, Object> {

	public static final String REGEX_PATTERN_ALPHANUM = "[-A-Za-z0-9_\\. ]*";
	private static Properties prop;
	@Override
	public void initialize(AlphaNum arg0) {

	}

	@Override
	public boolean isValid(Object value,
			ConstraintValidatorContext constraintContext) {
		return super.isValid(value,constraintContext);
	}

	/**
	 * 数值校验。参数值为null的情况，返回true。
	 * 
	 * @param value
	 *            校验对象
	 * @return 值继承了Number或者半角英数字的情况，返回true。
	 */
	public boolean valid(Object value) {
		if (value == null || value instanceof Number) {
			return false;
		}
		return ValidatorUtil.patternMatches(REGEX_PATTERN_ALPHANUM, value);
	}

	public String getMessage() {
		return prop
				.getProperty(this.getClass().getName());
	}

}
